<? 
	include('base.php');
	User::protect();
	include_class('band_information');
	include_class('shows');
	include_class('locations');
	include_class('venues');
	include_class('band_members');
	$section = 'shows';
	if ($_POST['submit']) { 
		// add news entry
		$sh = Show::add($_POST);
		if (!db::isError($sh)) {
			header('Location: show_edit.php?id=' . $sh->getID());
		}
	}	
	$editors = array('notes');
	$calendar = true;
	$javascript = 'venues.js';
	$page_title = 'Add Show';
	include('layout/header.php');
	$bi = BandInformation::get();
	$defaultStateProvince = $bi->getDefaultStateProvince();
	$defaultCity = $bi->getDefaultcity();
	$defaultCountry = $bi->getDefaultCountry();
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	Add Show
</div>
<?

if (!Show::canAdd()) {
	Error::outputDialog('Return to Shows','shows.php','You are not an administrator, and you are not a band member. Therefore, you cannot add shows.');
} else {
	
	if (db::isError($sh)) {
		$sh->outputList();
	}
?>
	<h1>new entry:</h1>
	<div class="inset">
	<form id="edit_entry" action="<?=$PHP_SELF?>?task=add" method="post">
	<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
	
	<?
		$sl = new VenueList();
		$limiter = array();
		$limiter['stateProvince'] = ($_REQUEST['stateProvince']) ? $_REQUEST['stateProvince'] : $defaultStateProvince;
		$limiter['city'] = ($_REQUEST['city']) ? $_REQUEST['city'] : $defaultCity;
		$venuelist = $sl->get($limiter);
		$splist = $sl->getStateProvinceList($defaultCountry);
		$limiter['city'] = null;
		$citylist = $sl->getCityList($limiter);
		
		$spl = new StateList();
		$ctl = new CountryList();
		$stateList = $spl->getStates();
		$countryList = $ctl->getCountries();
		$clist = $sl->getCountryList();
		
	?>
		
	<tr>
		<td width="50%"><strong>Country</strong><br/>
			<select name="country" id="countries">
				<option value="">** Select Country</option>
			<? foreach ($clist as $ct => $total) { ?>
				<option value="<?=$ct?>"<? if ($_REQUEST['country'] == $ct || (!$_REQUEST['country'] && $ct == $defaultCountry)) { ?> selected<? } ?>><?=$countryList[$ct]?> (<?=$total?>)</option>
			<? } ?>
			</select>
			</td>
		<td width="50%"><strong>State/Province</strong><br/>
		<select name="stateProvince" id="stateProvince">
			<option value="">** Select State/Province</option>
		<? foreach ($splist as $sp => $total) {
			$item = ($stateList[$sp] == null) ? $sp : $stateList[$sp]; ?>
			<option value="<?=$sp?>"<? if ($_REQUEST['stateProvince'] == $sp || (!$_REQUEST['stateProvince'] && $sp == $defaultStateProvince)) { ?> selected<? } ?>><?=$item?> (<?=$total?>)</option>
		<? } ?>
		</select>
		</td>
	</tr>
	<tr>
		<td width="50%"><strong>City</strong><br/>
		<select name="city" id="city">
			<option value="">** Select City</option>
		<? foreach ($citylist as $ci => $total) { ?>
			<option value="<?=$ci?>"<? if ($_REQUEST['city'] == $ci || (!$_REQUEST['city'] && $ci == $defaultCity)) { ?> selected<? } ?>><?=$ci?> (<?=$total?>)</option>
		<? } ?>
		</select>
		</td>
		<td width="50%"><strong>Venue</strong> (<a href="venue_add.php" target="_blank">Add Venue</a>)<br/>
		<select name="venue_id" id="venue">			
		<? foreach ($venuelist as $vn) { ?>
			<option value="<?=$vn->getID()?>"<? if ($vn->getID() == $_REQUEST['venue_id']) { ?> selected<? } ?>><?=$vn->getName()?></option>
		<? } ?>
			<option value="0"<? if ($_REQUEST['venue_id'] == 0) { ?> selected<? } ?>>** No Venue</option>
		</select>
		</td>
	</tr>
	<tr>
		<td colspan="3">You can leave the name of the show blank, and the name of the venue will be used.</td>
	</tr>
	</table>
	<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
	<tr>
		<td width="33%"><strong>Name</strong><br/><input type="text" name="name" value="<?=$_POST['name']?>" class="text"></td>
		<td width="34%"><strong>Date (<a style="vertical-align: middle" href="javascript:void(0)" onclick="popUpCalendar(this, document.getElementById('date'), '<?=DATE_JS_CALENDAR?>')">Calendar</a>)</strong><br/>
		<input name="date" id="date" class="text" type="text" value="<?=$_POST['date']?>">			
		</td>
		<td width="33%"><strong>Time</strong><br/><input type="text" name="time" value="<?=$_POST['time']?>"></td>
	</tr>
	<tr>
		<td><strong>Cost</strong><br/><input type="text" name="cost" value="<?=$_POST['cost']?>" class="text" ></td>
		
		<td><strong>Poster</strong><br/>		
		<? if (User::isAdmin()) { ?>
			<select name="user_id">
				<?
				$ul = new UserList();
				$uArray = $ul->getActiveAdminUsers();
				foreach ($uArray as $uo) { ?>
					<option class="admin-user" value="<?=$uo->getID()?>"<? if ($_POST['user_id'] == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?></option>
				<? }
				
				$bmlist = new BandMemberList();
				$bmarray = $bmlist->get_active_band_members();
				foreach ($bmarray as $bm) {
					$uo = $bm->getUserObject();
				?>
					<option value="<?=$uo->getID()?>"<? if ($_POST['user_id'] == $uo->getID()) { ?> SELECTED<? } ?>><?=$uo->getFirstName()?></option>
				<? } ?>
			</select>

		<? } else { 
			$uo = User::getCurrent();
			echo($uo->getFirstName());
		} ?>
		
		</td>
		<td><strong>Is All Ages?</strong><br/><input type="radio" name="is_all_ages" value="1"<? if ($_POST['is_all_ages']) { ?> checked<? } ?>> Yes
			<input type="radio" name="is_all_ages" value="0"<? if (!$_POST['is_all_ages']) { ?> checked<? } ?>> No				
		</td>
	</tr>
	</table>
	<div class="edit-form">
		<strong>Other Bands</strong><br/><input type="text" class="text" name="other_bands" value="<?=$_POST['other_bands']?>">
	</div>
	<div class="edit-form">
		<strong>Notes</strong><br/><textarea name="notes" id="notes" class="full"><?=$_POST['notes']?></textarea>
	</div>
	<div class="edit-form">
		<input type="submit" value="Add Show" class="button" name="submit">
		<input type="submit" value="Exit Without Adding" class="button" name="preview" onclick="location.href='shows.php'; return false">
	</div>
	</div>

	</div>
<? } ?>

<? include('layout/footer.php'); ?>
